const app=getApp();
var url = app.globalData.url;
var name1=""
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mainmenus: [{}, {}, {}, {}, {
      menuId: 0, "menuIcon": {
        "menuIconChecked":"../image/icon05.png",
        "menuIconUnChecked": "../image/icon05.png",
        "firstColor": "", 
        "lastColor": ""  
      },menuName:"常用服务"}],
    secmenus: [0,0],
     items:[],
    names:"匠在此",
    flag:false,
    toolIndex:0,
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      const that=this
    that.getMenuList();
    that.getComment();
    that.getBanner()
  },
  showPopup() {
    let popupComponent = this.selectComponent('.J_Popup');
    /*popupComponent && popupComponent.show();*/

    this.getUral()
    popupComponent && popupComponent.toggle(true);
  },
  hidePopup() {
    let popupComponent = this.selectComponent('.J_Popup');
    //popupComponent && popupComponent.hide();
    popupComponent && popupComponent.toggle();
  },

  popupTap() {
    let popupComponent = this.selectComponent('.J_Popup');

    popupComponent && popupComponent.toggle(false);
  },
  // 常用服务
  getUral(){
    const that = this
    wx.request({
      url: url + '/menu/menu_commonly_used',
      data: {},
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        console.log("获取常用服务：")
        console.log(res.data)

        if (res.data.code == 1) {
          that.setData({
            menuUral: res.data.data.menu
          })
        } else {
          wx.showToast({
            title: '加载失败',
            icon: "loading",
            duration: 1000
          })
        }

      }

    })  
  },
   getMenuList(){
     const that=this
     wx.request({
       url: url +'/menu/menus_one',
       data:{},
       header:{
         'content-type':'application/json'
       },
       success(res){
         console.log("获取一级菜单：")
         console.log(res.data)

         if(res.data.code==1){
           res.data.data.menus.push({
             menuId: 0, "menuIcon": {
               "menuIconChecked": "../image/icon05.png",
               "menuIconUnChecked": "../image/icon05.png",
               "firstColor": "",
               "lastColor": ""
             }, menuName: "常用服务"
           })
           that.setData({
             mainmenus: res.data.data.menus
           })
           name1 = res.data.data.menus[0].menuName
           console.log("赋值后的mainmenus：")
          //  console.log(this.data.mainmenus)
           that.getsecond(res.data.data.menus[0].menuId)
         } else {
              wx.showToast({
                title: '加载失败',
                icon:"loading",
                duration:1000
              })
         }

     }
      
     })  
   },
   getComment(){
     const that=this
     wx.request({
       url: url +'/comment/list_three',
       header:{
         "content-type":"application/json"
       },
       success(res){
         console.log("评论图片")
         console.log(res.data)
          if(res.data.code===1){
            that.data.items = res.data.data.comments
            that.setData({
              items: res.data.data.comments
            })
            // setInterval(function () {
            //   that.tabCar()
            //   console.log("动画循环")
            // }, 8000)
          }
       }
     })
   },
  getsecond(data){
    const that = this
  
    wx.request({
      url: url + '/menu/menus_pid',
      data: {
         pid:data
      },
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        console.log("获取二级菜单：")
        console.log(res.data)

        if(res.data.code==1){
            that.setData({
              secmenus: res.data.data.menus
            })
         
        }else{
          that.setData({
            secmenus:[]
          })
          wx.showToast({
            title: '暂无数据',
            icon: "loading",
            duration: 1000
          })
        }
      }
    })
  },
 
  tolist(e){
    console.log(e)
        wx.navigateTo({
          url: '../toOrder/toOrder?menuId=' + e.currentTarget.id + "&pid=" + e.currentTarget.dataset.pid + "&menutype=" + e.currentTarget.dataset.name + "&ordername=" + e.currentTarget.dataset.name + name1+"&pic="+e.currentTarget.dataset.pic,
        })
  
  },
  tosecMenu(e){
    var id = e.currentTarget.id
    const that = this
    that.sildeOut()

    that.getsecond(id)
 
    name1 = e.currentTarget.dataset.name

    setTimeout(function () {
      that.fadeOut()
    }, 500)
    that.setData({
      toolIndex:e.currentTarget.dataset.index
    })
   
  },
  tolist1(e){
    
    wx.navigateTo({
      url: '../toOrder/toOrder?menuId=' + e.currentTarget.dataset.id + "&pid=" + e.currentTarget.dataset.mid + "&menutype=" + e.currentTarget.dataset.name + "&ordername=" + e.currentTarget.dataset.name + e.currentTarget.dataset.pname+"&pic="+e.currentTarget.dataset.pic,
    })
    this.popupTap()
  },
  getBanner(){
    const that = this
    wx.request({
      url: url +'/banner/list',
      header:{
        'content-type':"application/json"
      },
      success(res){

        console.log("banner  list  is  :")
        console.log(res.data)
        if(res.data.code===0){
          that.setData({
            banner:res.data.data.banner
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  // 评论内容动画 
  tabCar(){
    //最下面
    var animationData0=wx.createAnimation({
      duration:300,
      timingFunction: 'cubic-bezier(.8,.2,.1,0.8)'
    })
    //中间
    var animationData1 = wx.createAnimation({
      duration: 200,
      timingFunction: 'cubic-bezier(.8,.2,.1,0.8)'
    })
    //最上面
    var animationData2 = wx.createAnimation({
      duration: 200,
      timingFunction: 'ease-in-out'
    })
    this.animationData0 = animationData0
    animationData0.translateY(80).translateX(0).step();
    animationData0.translateY(0).step()

    this.animationData1 = animationData1
  //  animationData1.translateY(0).step();
  //  animationData1.translateY(0).scaleX(0.997).opacity(0.6).step()

    this.animationData2 = animationData2
    animationData2.translateY(-80).step();
    animationData2.translateY(0).step()

    this.setData({
      animationData0: animationData0.export(),
    //   animationData1: animationData1.export(),
       animationData2: animationData2.export(),
       items:[this.data.items[1],this.data.items[2],this.data.items[0]]
    });
    

  },
  // 菜单事件
 sildeOut(){
   var animation = wx.createAnimation({
     duration: 100,
     timingFunction: "liner",
     delay: 0
   })
   this.animation = animation
   animation.translateX(800).opacity(0).step().translateX(-800).step()
   this.setData({
     animation: animation.export(),
   })
 },
 fadeOut(){
   var animation = wx.createAnimation({
     duration: 100,
     timingFunction: "liner",
     delay: 0
   })
   this.animation = animation
   animation.opacity(1).translateX().step()
   this.setData({
     animation: animation.export(),
   })

 },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this
    app.editTabBar();
  
  },
 
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})